package cn.com.lyj6851.auth.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.com.lyj6851.common.properties.RootSecurityProperties;

public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    //private org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    private ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private RootSecurityProperties securityProperties;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 当退出成功的时候，如果配置了一个页面，则跳转到页面，
        // 没有配置页面则打印session
        // 这里增加了一个属性，默认为空
        String signOutUrl = securityProperties.getBrowser().getSignOutUrl();
        if (StringUtils.isBlank(signOutUrl)) {
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString("退出成功"));
        } else {
            response.sendRedirect(signOutUrl);
        }
    }

   
}
